package com.example.mychatroom.serviceImpl;

import com.example.mychatroom.mapper.FriendMapper;
import com.example.mychatroom.mapper.MessageMapper;
import com.example.mychatroom.model.Friend;
import com.example.mychatroom.model.MessageSession;
import com.example.mychatroom.model.MessageSessionUserItem;
import com.example.mychatroom.serviceApi.FriendService;
import com.example.mychatroom.mapper.MessageSessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @className: FriendServiceImpl
 * @author: 十三
 * @description:
 * @date: 2025/3/18 12:36
 */
@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private MessageSessionMapper messageSessionMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public List<Friend> getFriendList(String userAccount) {
        List<Friend> friendList = new ArrayList<Friend>();
        friendList = friendMapper.selectFriendList(userAccount);
        return friendList;
    }

    @Override
    public List<MessageSession> getSessionList(String userAccount) {
        //结果集
        List<MessageSession> messageSessionList = new ArrayList<MessageSession>();
        //根据userAccount查询数据库，查出来有哪些会话ID
        List<Integer> sessionList = messageSessionMapper.getSessionIdsByUserAccount(userAccount);
        for (Integer id : sessionList) {
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(id);
            //遍历会话Id，查询出每个会话里涉及到的好友都有谁
            List<Friend> friendList = messageSessionMapper.getFriendBySessionId(id,userAccount);
            messageSession.setFriends(friendList);
            //遍历会话id，查询出每个会话的最后一条消息是什么
            String lastMessage = messageMapper.getLastMessageBySessionId(id);
            if (lastMessage != null){
                messageSession.setLastMessage(lastMessage);
            }else {
                //可能新创建的会话还没消息
                messageSession.setLastMessage(null);
            }
            messageSessionList.add(messageSession);
        }
        //将查询出来的结果封装到messageSessionList中
        return messageSessionList;
    }

    @Override
    public HashMap<String, Integer> createSession(String userAccount, String toUserAccount) {
        MessageSession messageSession = new MessageSession();
        //1、先在message_session 表里新增一个会话数据
        //  换而言之，MessageSession里的friend和lastMessage都用不上
        messageSessionMapper.addMessageSession(messageSession);
        //2、给message_session_user 表里插入数据
        MessageSessionUserItem item1 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserAccount(userAccount);
        messageSessionMapper.addMessageSessionUser(item1);
        //3、给message_session_user 表里插入数据
        MessageSessionUserItem item2 = new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserAccount(toUserAccount);
        messageSessionMapper.addMessageSessionUser(item2);
        System.out.println("createSession 新增会话成功,sessionId = " + messageSession.getSessionId() + " userAccount1 = " +userAccount + ",userAccount2 = " +  toUserAccount);
        //4、返回会话ID
        HashMap<String,Integer> resp = new HashMap<String,Integer>();
        resp.put("sessionId",messageSession.getSessionId());
        return resp;
    }
}
